<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Create trade' );

$result2 = mysql_query( "SELECT gold " .
		"FROM ruler " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$gold_total = $row2[ 'gold' ];
?>
					<table id="values">
<?php
$result2 = mysql_query( "SELECT coal_gold, iron_gold, food_gold " .
		"FROM game" ) or die ( "Query failed: " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$gold_iron = 1 / $row2[ 'iron_gold' ];
$gold_coal = 1 / $row2[ 'coal_gold' ];
$gold_food = 1 / $row2[ 'food_gold' ];
$iron_coal = $row2[ 'iron_gold' ] / $row2[ 'coal_gold' ];
$coal_iron = 1 / $iron_coal;
$food_coal = $row2[ 'food_gold' ] / $row2[ 'coal_gold' ];
$coal_food = 1 / $food_coal;
$iron_food = $row2[ 'iron_gold' ] / $row2[ 'food_gold' ];
$food_iron = 1 / $iron_food;
?>
						<tr>
							<td width="25%">&nbsp;</td>
							<td align="right">
								<strong class="gold"><?php transprint( 'Gold' ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php transprint( 'Iron' ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php transprint( 'Coal' ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php transprint( 'Food' ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="gold"><?php transprint( 'Gold' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold">1</strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $gold_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $gold_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $gold_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="iron"><?php transprint( 'Iron' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'iron_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong id="iron">1</strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $iron_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $iron_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="coal"><?php transprint( 'Coal' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'coal_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $coal_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal">1</strong>
							</td>
							<td align="right">
								<strong class="food"><?php print( round( $coal_food, 2 ) ); ?></strong>
							</td>
						</tr>
						<tr>
							<td>
								<strong class="food"><?php transprint( 'Food' ); ?></strong>
							</td>
							<td align="right">
								<strong class="gold"><?php print( round( $row2[ 'food_gold' ], 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="iron"><?php print( round( $food_iron, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="coal"><?php print( round( $food_coal, 2 ) ); ?></strong>
							</td>
							<td align="right">
								<strong class="food">1</strong>
							</td>
						</tr>
					</table>
<?php
include( "../global/resources.php" );
?>
					<div class="clear"></div>
					<br />
<?php
if( isset( $_SESSION[ 'create_trade_status' ] ) ) {
?>
				<div class="blok">
					<?php transprint( 'Status' ); ?>: <?php print( $_SESSION[ 'create_trade_status' ] ); ?><br />
<?php
	if( isset( $_SESSION[ 'trade_offer_rating' ] ) ) {
?>
					<?php transprint( 'This trade offer will be rated as' ); ?>: <?php print( $_SESSION[ 'trade_offer_rating' ] ); ?>
<?php
		unset( $_SESSION[ 'trade_offer_rating' ] );
	}
?>
				</div>
				<br />
<?php
		unset( $_SESSION[ 'create_trade_status' ] );
	}
	$gold_offer = getSessionValue( 'gold_offer' );
	$iron_offer = getSessionValue( 'iron_offer' );
	$coal_offer = getSessionValue( 'coal_offer' );
	$food_offer = getSessionValue( 'food_offer' );
	$gold_request = getSessionValue( 'gold_request' );
	$iron_request = getSessionValue( 'iron_request' );
	$coal_request = getSessionValue( 'coal_request' );
	$food_request = getSessionValue( 'food_request' );
	
	$value_offered = getSessionValue( 'value_offered' );
	$value_requested = getSessionValue( 'value_requested' );
	$access = getSessionValue( 'trade_access' );
	$access_name = getSessionValue( 'trade_access_name' );	
	$trade_as = getSessionValue( 'trade_as' );
	$trade_length = getSessionValue( 'trade_length' );
	if( !isset( $access_name ) || $access_name == 0 ) {
		$access_name = '';
	}
?>
	
				<form action="create_trade2.php" method="post">
					<table class="blank"> 
						<tr>
							<th colspan="2"><?php transprint( 'Offered' ); ?></th>
							<th colspan="2"><?php transprint( 'Requested' ); ?></th>
						</tr> 
						<tr>
							<td><strong class="gold"><?php transprint( 'Gold' ); ?></strong></td>
							<td><input type="text" size="5" name="gold_offer" value="<?php print( $gold_offer ); ?>" /></td>
		      			<td><strong class="gold"><?php transprint( 'Gold' ); ?></strong></td>
		      			<td><input type="text" size="5" name="gold_request" value="<?php print( $gold_request ); ?>" /></td>
		      		</tr>
						<tr>
							<td><strong class="iron"><?php transprint( 'Iron' ); ?></strong></td>
							<td><input type="text" size="5" name="iron_offer" value="<?php print( $iron_offer ); ?>" /></td>
			  				<td><strong class="iron"><?php transprint( 'Iron' ); ?></strong></td>
			  				<td><input type="text" size="5" name="iron_request" value="<?php print( $iron_request ); ?>" /></td>
			  			</tr>
						<tr>
							<td><strong class="coal"><?php transprint( 'Coal' ); ?></strong></td>
							<td><input type="text" size="5" name="coal_offer" value="<?php print( $coal_offer ); ?>" /></td>
			  				<td><strong class="coal"><?php transprint( 'Coal' ); ?></strong></td>
			  				<td><input type="text" size="5" name="coal_request" value="<?php print( $coal_request ); ?>" /></td>
			  			</tr>
						<tr>
							<td><strong class="food"><?php transprint( 'Food' ); ?></strong></td>
							<td><input type="text" size="5" name="food_offer" value="<?php print( $food_offer ); ?>" /></td>
			  				<td><strong class="food"><?php transprint( 'Food' ); ?></strong></td>
			  				<td><input type="text" size="5" name="food_request" value="<?php print( $food_request ); ?>" /></td>
			  			</tr>
						<tr>
							<td><?php transprint( 'Total value in gold' ); ?></td>
							<td><?php print( number_format( $value_offered ) ); ?></td>
			  				<td><?php transprint( 'Total value in gold' ); ?></td>
			  				<td><?php print( number_format( $value_requested ) ); ?></td>
			  			</tr>
					</table>
					<br />
					<div class="blok">
<?php
	$result2 = mysql_query( "SELECT faction_id FROM faction WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	if( isset( $row2[ 'faction_id' ] ) ) {
		print( "<strong>" . translate( 'Trade as?' ) . "</strong><br />\r\n" );
		$checked = "";
		if( $trade_as == '0' ) {
			$checked = " checked=\"checked\"";
		}
		print( "<input type=\"radio\"$checked value=\"0\" name=\"from\" control=\"from\" /> " . translate( 'Trade as ruler' ) . "<br />\r\n" );
		$checked = "";
		if( $trade_as == '1' ) {
			$checked = " checked=\"checked\"";
		}
		print( "<input type=\"radio\"$checked value=\"1\" name=\"from\" control=\"from\" /> " . translate( 'Trade as faction' ) . "\r\n<br />\r\n<br />\r\n" );
	}
	
	print( "<strong>" . translate( 'Trade offer shown' ) . "</strong><br />\r\n" );
	$checked = "";
	if( $access == '0' ) {
		$checked = " checked=\"checked\"";
	}
	print( "<input type=\"radio\"$checked value=\"0\" name=\"type\" control=\"type\" onchange=\"setDisabled( true );\" /> " . translate( 'Public trade offer' ) . "<br />\r\n" );
	$result2 = mysql_query( "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	if( isset( $row2[ 'faction_id' ] ) ) {
		$checked = "";
		if( $access == '1' ) {
			$checked = " checked=\"checked\"";
		}
		print( "<input type=\"radio\"$checked value=\"1\" name=\"type\" control=\"type\" onchange=\"setDisabled( true );\" /> " . translate( 'Faction trade offer' ) . "<br />\r\n" );
	}
	$checked = "";
	$disabled = " disabled=\"true\"";
	if( $access == '2' ) {
		$checked = " checked=\"checked\"";
		$disabled = "";
	}
	print( "<input type=\"radio\"$checked value=\"2\" name=\"type\" control=\"type\" onchange=\"setDisabled( false );\" /> " . translate( 'Private trade offer for' ) . ": " .
		   "<input type=\"text\" id=\"private_name\" name=\"private_name\" size=\"12\"$disabled value=\"$access_name\"/><br />\r\n" );
	print( "<br />\r\n" );
	
	print( "<strong>" . translate( 'Offer stays open for' ) . "</strong><br />\r\n" );
	$checked = "";
	if( $trade_length == '0' ) {
		$checked = " checked=\"checked\"";
	}
	print( "<input type=\"radio\"$checked value=\"0\" name=\"length\" control=\"length\" /> " . translate( '24 days, free' ) . "<br />\r\n" );
	if( $gold_total > 10 ) {
		$checked = "";
		if( $trade_length == '1' ) {
			$checked = " checked=\"checked\"";
		}
		print( "<input type=\"radio\"$checked value=\"1\" name=\"length\" control=\"length\" /> " . translate( '48 days, <strong class="gold">10</strong> gold' ) . "<br />\r\n" );
	}
	if( $gold_total > 20 ) {
		$checked = "";
		if( $trade_length == '2' ) {
			$checked = " checked=\"checked\"";
		}
		print( "<input type=\"radio\"$checked value=\"2\" name=\"length\" control=\"length\" /> " . translate( '96 days, <strong class="gold">20</strong> gold' ) . "<br />\r\n" );
	}
	
	$multiplier = getSessionValue( 'multiplier' );
	if( $multiplier < 1 ) {
		$multiplier = 1;
	}
	elseif( $multiplier > 5 ) {
		$multiplier = 5;
	}
?>
						<br />
						<?php transprint( 'Create this trade' ); ?> <input type="textfield" size="3" name="multiplier" value="<?php print( $multiplier ); ?>" /> <?php transprint( 'time(s)' ); ?><br />
						<small>(<?php transprint( 'If you want to create this trade more then once you can set it here. Default is 1 time, maximum is 5 times' ); ?>)</small><br />
						<br />
						<input type="submit" name="calculate" value="<?php transprint( 'Trade offer evaluation' ); ?>" />
						<input type="submit" name="submit" value="<?php transprint( 'Create trade offer' ); ?>" />
					</div>
				</form>
				<script type="text/javascript">
				function setDisabled( value ) {
					var tmp = document.getElementById( "private_name" );
					if( tmp ) {
						if( value ) {
							tmp.disabled = true;
						}
						else {
							tmp.disabled = false;
						}
					}
				}
				</script>
<?php
aos_footer();
?>